package com.changgou.seckill.timer;

import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import entity.DateUtil;
import entity.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.seckill.timer *
 * @Date 2019-12-10
 * @since 1.0
 */
@Component
public class SeckillGoodsPushTask {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    //从0秒开始 每隔5秒执行一次
    @Scheduled(cron = "1/5 * * * * ? ")//设置定时任务注解,表示方法 定时的去执行,定义何时执行的表达式:CRON
    public void pushGoodsToRedis() {
        List<Date> dateMenus = DateUtil.getDateMenus();
        //循环时间段
        for (Date startTime : dateMenus) {//12 ----20
            // namespace = SeckillGoods_20195712
            String extName = DateUtil.data2str(startTime,DateUtil.PATTERN_YYYYMMDDHH);

            //根据时间段数据查询对应的秒杀商品数据
            Example example = new Example(SeckillGoods.class);
            Example.Criteria criteria = example.createCriteria();
            // 1)商品必须审核通过  status=1
            criteria.andEqualTo("status","1");
            // 2)库存>0
            criteria.andGreaterThan("stockCount",0);
            // 3)开始时间<=活动开始时间
            criteria.andGreaterThanOrEqualTo("startTime",startTime);
            // 4)活动结束时间<开始时间+2小时
            criteria.andLessThan("endTime", DateUtil.addDateHour(startTime,2));


            //排除掉在redis中已有的数据
            Set keys = redisTemplate.boundHashOps("SeckillGoods_" + extName).keys();
            // and in not in (redis中的id)
            if(keys!=null && keys.size()>0) {
                criteria.andNotIn("id", keys);
            }
            //查询数据 key:时间段  field id  value:goods数据
            List<SeckillGoods> seckillGoods = seckillGoodsMapper.selectByExample(example);

            //将秒杀商品数据存入到Redis缓存
            for (SeckillGoods seckillGood : seckillGoods) {
                //设置数据
                redisTemplate.boundHashOps("SeckillGoods_"+extName).put(seckillGood.getId(),seckillGood);
                //设置key的有效期
                redisTemplate.boundHashOps("SeckillGoods_"+extName).expireAt(DateUtil.addDateHour(startTime,2));

                //压入到队列中
                pushGoodsToRedisList(seckillGood);

            }
        }

    }

    private void pushGoodsToRedisList(SeckillGoods seckillGoods){
        for (Integer i = 0; i < seckillGoods.getStockCount(); i++) {
            redisTemplate.boundListOps(SystemConstants.SEC_KILL_CHAOMAI_LIST_KEY_PREFIX+seckillGoods.getId()).leftPush(seckillGoods.getId());
        }

    }

    public static void main(String[] args) {
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date dateMenu : dateMenus) {
            String startTimeString = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYYMMDDHH);
            System.out.println(startTimeString);
        }
    }
}
